<template>
  <div class="app-container calendar-list-container">
    <!--汇率设置-->
    <div class="apply-query">
      <el-row class="filter-query">
        <el-col><span>{{ $t('management.costWhiteList.queryCriteria') }}</span></el-col>
      </el-row>

      <el-form :inline="true" :model="listQuery" style="padding: 10px 20px 0">
        <el-row style="padding-bottom: 10px">
          <el-col :span="4">
            <div style="margin-right: 40px">
              <div class="labelName">{{ $t('exchangeRate.country') }}</div>
              <dict-select v-model="listQuery.country" code="country" :clearable="true"></dict-select>
            </div>
          </el-col>
          <el-col :span="4" style="margin-top: 23px">
            <div type="primary" class="yt-btn yt-btn-ys" @click="handleFilter">
              {{ $t('button.query') }}
            </div>
            <div type="primary" class="yt-btn yt-ml8" @click="reset">
              {{ $t('button.reset') }}
            </div>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="yt-data-list">
      <div class="yt-list-title">
        <span>{{ $t('exchangeRate.rateSetList') }}</span>
      </div>
      <div class="yt-list-content">
        <div class="list-operation">
          <!--新增-->
          <div class="yt-btn yt-btn-ys" type="primary" @click="handleAdd">
            {{ $t('button.new') }}
          </div>
          <!--删除-->
          <div class="yt-btn yt-ml8" type="danger" @click="handleDelete">
            {{ $t('button.delete') }}
          </div>
        </div>

        <el-table :data="list" :empty-text="$t('placeholder.emptyText')" border style="width: 100%;" class="yt-table" @row-click="clickRow" @selection-change="handleSelectionChange" v-loading.body="listLoading" max-height="600">
          <el-table-column type="selection" align="center" prop="id" width="60"/>
          <el-table-column type="index" align="center" :label="$t('management.costWhiteList.serialNumber')" width="60"/>
          <el-table-column align="center" :label="$t('exchangeRate.country')" prop="countryName"/>
          <el-table-column align="center" :label="$t('exchangeRate.rate')" prop="rate"/>
          <el-table-column align="center" :label="$t('exchangeRate.updateName')" prop="updateName"/>
          <el-table-column align="center" :label="$t('exchangeRate.updateTime')" prop="updateTime"/>
        </el-table>
        <div class="pagination-container" >
          <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page.sync="listQuery.page"
            :page-sizes="[10, 20, 30, 50,100, 200]"
            :page-size="listQuery.limit"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
          >
          </el-pagination>
        </div>
      </div>
    </div>
    <!--弹窗-->
    <el-dialog :title="$t('exchangeRate.addData')" :visible.sync="dictDialogVisible">
      <el-form ref="form" :model="dialogForm" label-width="105px">
        <el-form-item :label="$t('exchangeRate.country')">
          <template>
            <dict-select v-model="dialogForm.country" code="country" :clearable="true"></dict-select>
          </template>
        </el-form-item>
        <el-form-item :label="$t('exchangeRate.rate')">
          <el-input type="number" v-model="dialogForm.rate"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button class="yt-btn" @click="resetForm">{{$t('button.cancel')}}</el-button>
        <el-button class="yt-btn-ys" type="primary" @click="editorDict">{{$t('button.determine')}}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>

  import { queryList, insert, update, batchDelete } from '@/api/pmoReport/exchangeRate';
  import dictSelect from '@/components/Dict/dictValueSelect';

  export default {
    name: 'notAccount',
    data() {
      return {
        listLoading: true,
        listQuery: {
          page: 1,
          limit: 10,
          country: ''
        },
        total: 0,
        userList: [],
        list: [],
        selectionList: [],
        dictDialogVisible: false,
        dialogForm: { country: '', id: '', rate: null }
      };
    },
    components: { dictSelect },
    created() {
      this.getList()
    },
    methods: {
      editorDict() {
        if (this.dialogForm.id) {
          update(this.dialogForm).then(res => {
            this.$notify({
              title: res.status === 200 ? this.$t('tips.editSuccess') : this.$t('tips.success'),
              message: res.status === 201 ? this.$t('tips.editFail') + res.data : this.$t('tips.success'),
              type: res.status === 200 ? 'success' : 'warning'
            });
            if (res.status === 200) {
              this.resetForm();
              this.dictDialogVisible = false;
              this.getList();
            }
          });
        } else {
          insert(this.dialogForm).then(res => {
            this.$notify({
              title: res.status === 200 ? this.$t('tips.editSuccess') : this.$t('tips.success'),
              message: res.status === 201 ? this.$t('tips.editFail') + res.data : this.$t('tips.success'),
              type: res.status === 200 ? 'success' : 'warning'
            });
            if (res.status === 200) {
              this.resetForm();
              this.dictDialogVisible = false;
              this.getList();
            }
          });
        }
      },
      getList() {
        this.listLoading = true;
        queryList(this.listQuery).then(res => {
          this.list = res.data.data.rows
          this.total = res.data.data.total
          this.list.forEach(v => {
            v.countryName = this.$store.getters.allMapDic.country[v.country]
          })
          this.listLoading = false
        })
      },
      handleAdd() {
        this.dictDialogVisible = true;
        this.dialogForm = { country: '', id: '', rate: null }
      },
      handleDelete() {
        if (this.selectionList.length <= 0) {
          this.$notify({
            title: this.$t('tips.fail'),
            message: this.$t('tips.pleaseChoose'),
            type: 'warning'
          });
          return;
        }
        this.$confirm(this.$t('tips.permanentDelete'), this.$t('tips.tips'), {
          confirmButtonText: this.$t('tips.determine'),
          cancelButtonText: this.$t('tips.cancel'),
          type: 'warning'
        }).then(() => {
          const list = this.selectionList.map(item => item.id)
          batchDelete(list).then(() => {
            this.$notify({
              title: this.$t('tips.success'),
              message: this.$t('tips.deleteSuccess'),
              type: 'success',
              duration: 2000
            });
            this.getList();
          });
        });
      },
      handleSizeChange(val) {
        this.listQuery.limit = val;
        this.getList();
      },
      handleCurrentChange(val) {
        this.listQuery.page = val;
        this.getList();
      },
      handleSelectionChange(val) {
        //  表格序号选中函数
        this.selectionList = val;
      },
      reset() {
        this.listQuery = { page: 1, limit: 10, country: '' };
        this.getList();
      },
      clickRow(row, event, column) {
        if (column.property !== 'id') {
          this.dialogForm.id = row.id
          this.dialogForm.country = row.country
          this.dialogForm.rate = row.rate
          this.dictDialogVisible = true;
        }
      },
      resetForm() {
        this.dictDialogVisible = false;
        this.dialogForm = {};
      },
      handleFilter() {
        this.getList();
      }
    }
  };
</script>
<style scoped>
</style>
